package com.wlcorp.forcazamis.sys.util;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateUtil {

	private static final String Simple_Date_Format = "yyyy-MM-dd";
	private static final int Simple_Date_Format_Length = Simple_Date_Format
			.length();
	private static final String Simple_DateTime_Format = "yyyy-MM-dd HH:mm:ss";

	public static java.util.Date stringToUtilDate(String str) {
		str.replaceAll("/", "-");
		if (null != str && str.length() > 0) {
			try {
				// ��}��ʱ���ʽ����ת����
				if (str.length() <= Simple_Date_Format_Length) { // ֻ�����ڡ�
					return (new SimpleDateFormat(Simple_Date_Format))
							.parse(str);
				} else { // ���ں�ʱ�䶼��
					return (new SimpleDateFormat(Simple_DateTime_Format))
							.parse(str);
				}
			} catch (ParseException ex) {
				return null;
			}
		} else
			return null;
	}

	public static java.sql.Date stringToSqlDate(String str) {

		if (stringToUtilDate(str) == null || str.length() < 1)
			return null;
		else
			return new java.sql.Date(stringToUtilDate(str).getTime());
	}

	public static String toDateTimeString(java.sql.Date d) {
		if (d == null) {
			return null;
		} else {
			return (new SimpleDateFormat(Simple_DateTime_Format)).format(d);
		}
	}

	public static String toDateTimeString(java.util.Date d) {
		if (d == null) {
			return null;
		} else {
			return (new SimpleDateFormat(Simple_DateTime_Format)).format(d);
		}
	}

	public static String toDateString(java.sql.Date d) {
		if (d == null) {
			return null;
		} else {
			return (new SimpleDateFormat(Simple_Date_Format)).format(d);
		}
	}

	public static String toDateString(java.util.Date d) {
		if (d == null) {
			return null;
		} else {
			return (new SimpleDateFormat(Simple_Date_Format)).format(d);
		}
	}

	public static java.sql.Date getCurrentDate() {
		/** todo Ҫ�óɻ�ȡ��ǰʱ�䡣 */
		return new java.sql.Date(new java.util.Date().getTime());
	}

	public static java.sql.Date utilToSql(java.util.Date date) {
		return new java.sql.Date(date.getTime());
	}

	public static java.util.Date sqlToUtil(java.sql.Date date) {
		return new java.util.Date(date.getTime());
	}

	public static String getMonthDay(java.sql.Date date) {
		if (null == date)
			return null;
		Calendar canendar = Calendar.getInstance();
		canendar.setTime(date);
		return String.valueOf(canendar.get(Calendar.MONTH) + 1) + "."
				+ String.valueOf(canendar.get(Calendar.DATE));
	}

	public static java.sql.Date compositeDateTime(java.sql.Date date,
			java.sql.Time time) {
		if (null == date || null == time)
			return null;
		Calendar calDate = new GregorianCalendar();
		calDate.setTimeInMillis(date.getTime());

		Calendar calTime = new GregorianCalendar();
		calTime.setTimeInMillis(time.getTime());

		Calendar calCompositeDateTime = new GregorianCalendar();
		int iYear = calDate.get(Calendar.YEAR);
		int iMonth = calDate.get(Calendar.MONTH);
		int iDay = calDate.get(Calendar.DATE);
		int iHour = calTime.get(Calendar.HOUR_OF_DAY);
		int iMin = calTime.get(Calendar.MINUTE);
		int iSec = calTime.get(Calendar.SECOND);
		int iMSec = calTime.get(Calendar.MILLISECOND);
		calCompositeDateTime.set(iYear, iMonth, iDay, iHour, iMin, iSec);
		calCompositeDateTime.set(Calendar.MILLISECOND, iMSec);
		return utilToSql(calCompositeDateTime.getTime());
	}

	public static java.sql.Date getDateTime() {
		Calendar newcalendar = null;
		newcalendar = Calendar.getInstance();

		String year = String.valueOf(newcalendar.get(Calendar.YEAR));
		String month = String.valueOf(newcalendar.get(Calendar.MONTH) + 1);
		String day = String.valueOf(newcalendar.get(Calendar.DAY_OF_MONTH));
		String hour = String.valueOf(newcalendar.get(Calendar.HOUR_OF_DAY));
		String min = String.valueOf(newcalendar.get(Calendar.MINUTE));
		String sec = String.valueOf(newcalendar.get(Calendar.MINUTE));

		return stringToSqlDate(year + "-" + month + "-" + day + " " + hour
				+ ":" + min + ":" + sec);
	}

	public static int getSlipDays(String beginDateStr, String endDateStr) {
		java.sql.Date beginDate = stringToSqlDate(beginDateStr);
		java.sql.Date endDate = stringToSqlDate(endDateStr);
		long slipDateTime = endDate.getTime() - beginDate.getTime();
		return (int) slipDateTime / (1000 * 3600 * 24);
	}

	/**
	 * 
	 * @������ƣ�getNextYearTime
	 * @��������
	 * @param beginTime
	 * @return
	 */
	public static Date getNextYearTime(Date beginTime, int i) {
		Calendar date = Calendar.getInstance();
		date.setTime(beginTime);
		date.add(Calendar.YEAR, i);
		return utilToSql(date.getTime());
	}

	/**
	 * 
	 * @������ƣ�getNextMonthTime
	 * @��������
	 * @param beginTime
	 * @return
	 */
	public static Date getNextMonthTime(Date beginTime, int i) {
		Calendar date = Calendar.getInstance();
		date.setTime(beginTime);
		date.add(Calendar.MONTH, i);
		return utilToSql(date.getTime());
	}

	/**
	 * 
	 * @������ƣ�getNextDayTime
	 * @��������
	 * @param beginTime
	 * @return
	 */
	public static Date getNextDayTime(Date beginTime, int i) {
		Calendar date = Calendar.getInstance();
		date.setTime(beginTime);
		date.add(Calendar.DAY_OF_MONTH, i);
		return utilToSql(date.getTime());
	}

	/**
	 * 
	 * @������ƣ�compareYear
	 * @��������Ƚ���
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static int compareYear(Date beginTime, Date endTime) {
		Calendar begin = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		begin.setTime(beginTime);
		end.setTime(endTime);
		int compareYear = end.get(Calendar.YEAR) - begin.get(Calendar.YEAR);
		return compareYear;
	}

	/**
	 * 
	 * @������ƣ�compareMonth
	 * @��������Ƚ���
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static int compareMonth(Date beginTime, Date endTime) {
		int compareYear = compareYear(beginTime, endTime);
		Calendar begin = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		begin.setTime(beginTime);
		end.setTime(endTime);
		int compareMonth = compareYear * 12
				+ (end.get(Calendar.MONTH) - begin.get(Calendar.MONTH));
		return compareMonth;
	}

	/**
	 * 
	 * @������ƣ�compareDay
	 * @��������Ƚ���
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static int compareDay(Date beginTime, Date endTime) {
		long compare = (endTime.getTime() - beginTime.getTime())
				/ (1000 * 3600 * 24);
		String compareStr = String.valueOf(compare);
		return Integer.parseInt(compareStr);
	}

	public static boolean compareDate(java.util.Date beginTime,
			java.util.Date endTime) {

		if (compareYear(utilToSql(beginTime), utilToSql(endTime)) != 0)
			return false;
		if (compareMonth(utilToSql(beginTime), utilToSql(endTime)) != 0)
			return false;
		if (compareDay(utilToSql(beginTime), utilToSql(endTime)) != 0)
			return false;

		return true;
	}

	public static String currentDate() {
		Calendar calendar = Calendar.getInstance();
		java.util.Date date = calendar.getTime();
		SimpleDateFormat sf = new SimpleDateFormat("yyMMddHHmmss");
		return sf.format(date);
	}
}
